package com.ipan.poi.excel.exporter;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 默认格式化实现；
 * 
 * @author iPan
 * @version 2015-01-10
 */
public class DefaultCellStyleCreater implements CellStyleCreatable {
	/** 标题字体 */
	protected static final String DEFAULT_TITLE_FONTNAME = "宋体";
	/** 标题字号 */
	protected static final short DEFAULT_TITLE_FONTSIZE = 12;
	/** 正文字体 */
	protected static final String DEFAULT_BODY_FONTNAME = "宋体";
	/** 正文字号 */
	protected static final short DEFAULT_BODY_FONTSIZE = 10;

	/** 标题背景色 */
	protected Short titleBackgroundColor = null;
	/** 正文背景色 */
	protected Short bodyBackgroundColor = null;
	/** 标题字体 */
	protected Font titleFont;
	/** 正文字体 */
	protected Font bodyFont;
	/** 标题样式 */
	protected CellStyle titleStyle; // 2015-01-10修正：字体、样式的使用有限制，需要改成重用；
	/** 正文样式 */
	protected CellStyle bodyStyle; // // 2015-01-10修正
	
	public DefaultCellStyleCreater() {
	}

	/**
	 * 构造时候可指定颜色
	 * 
	 * @param titleBackgroundColor 标题颜色
	 * @see org.apache.poi.ss.usermodel.IndexedColors
	 */
	public DefaultCellStyleCreater(short titleBackgroundColor) {
		this.titleBackgroundColor = titleBackgroundColor;
	}
	
	public DefaultCellStyleCreater(short titleBackgroundColor, short bodyBackgroundColor, Font titleFont, Font bodyFont) {
		this.titleBackgroundColor = titleBackgroundColor;
		this.bodyBackgroundColor = bodyBackgroundColor;
		this.titleFont = titleFont;
	}

	public CellStyle createTitleStyle(Workbook workbook) {
		if (titleStyle == null) {
			titleStyle = this.createDefaultTitleStyle(workbook);
		}
		return titleStyle;
	}

	public CellStyle createBodyStyle(Workbook workbook) {
		if (bodyStyle == null) {
			bodyStyle = this.createDefaultBodyStyle(workbook);
		}
		return bodyStyle;
	}

	public Short getTitleBackgroundColor() {
		return titleBackgroundColor;
	}

	public void setTitleBackgroundColor(short titleBackgroundColor) {
		this.titleBackgroundColor = titleBackgroundColor;
	}

	public Short getBodyBackgroundColor() {
		return bodyBackgroundColor;
	}

	public void setBodyBackgroundColor(short bodyBackgroundColor) {
		this.bodyBackgroundColor = bodyBackgroundColor;
	}

	public Font getTitleFont() {
		return titleFont;
	}

	public void setTitleFont(Font titleFont) {
		this.titleFont = titleFont;
	}

	public Font getBodyFont() {
		return bodyFont;
	}

	public void setBodyFont(Font bodyFont) {
		this.bodyFont = bodyFont;
	}

	public void setTitleBackgroundColor(Short titleBackgroundColor) {
		this.titleBackgroundColor = titleBackgroundColor;
	}

	public void setBodyBackgroundColor(Short bodyBackgroundColor) {
		this.bodyBackgroundColor = bodyBackgroundColor;
	}
	
	private CellStyle createDefaultTitleStyle(Workbook workbook) {
		CellStyle style = null;
		style = workbook.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		if (titleBackgroundColor != null) {
			style.setFillPattern(CellStyle.SOLID_FOREGROUND);
			style.setFillForegroundColor(titleBackgroundColor);
		}
		style.setBorderBottom(CellStyle.BORDER_THIN);
		style.setBorderTop(CellStyle.BORDER_THIN);
		style.setBorderLeft(CellStyle.BORDER_THIN);
		style.setBorderRight(CellStyle.BORDER_THIN);
		if (titleFont == null) {
			this.titleFont = createDefaultTitleFont(workbook);
		}
		style.setFont(titleFont);
		return style;
	}
	
	private CellStyle createDefaultBodyStyle(Workbook workbook) {
		CellStyle style = null;
		style = workbook.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_LEFT);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		if (this.bodyBackgroundColor != null) {
			style.setFillPattern(CellStyle.SOLID_FOREGROUND);
			style.setFillForegroundColor(bodyBackgroundColor);
		}
		style.setBorderBottom(CellStyle.BORDER_THIN);
		style.setBorderTop(CellStyle.BORDER_THIN);
		style.setBorderLeft(CellStyle.BORDER_THIN);
		style.setBorderRight(CellStyle.BORDER_THIN);
		if (bodyFont == null) {
			this.bodyFont = createDefaultBodyFont(workbook);
		}
		style.setFont(bodyFont);
		return style;
	}

	/**
	 * 创建默认标题字体
	 */
	public static Font createDefaultTitleFont(Workbook workbook) {
		return createFont(workbook, DEFAULT_BODY_FONTNAME, DEFAULT_BODY_FONTSIZE, Font.BOLDWEIGHT_BOLD);
	}
	
	/**
	 * 创建默认正文字体
	 */
	public static Font createDefaultBodyFont(Workbook workbook) {
		return createFont(workbook, DEFAULT_BODY_FONTNAME, DEFAULT_BODY_FONTSIZE, Font.BOLDWEIGHT_NORMAL);
	}
	
	/**
	 * 创建字体
	 */
	public static Font createFont(Workbook workbook, String fontName, short fontSize, short fontWeight) {
		Font font = workbook.createFont();
		font.setFontName(fontName);
		font.setFontHeightInPoints(fontSize);
		font.setBoldweight(fontWeight);
		return font;
	}
	
}
